home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 401-425 / disk_419 / yacc / src.lzh / Src / main.c < prev    next >
C/C++ Source or Header  |  1990-07-14  |  7KB  |  366 lines

  1. #include <signal.h>
  2. #include "defs.h"
  3.  
  4. char dflag;
  5. char lflag;
  6. char rflag;
  7. char tflag;
  8. char vflag;
  9.  
  10. char *file_prefix = "y";
  11. char *myname = "yacc";
  12. #ifndef AMIGA
  13. char *temp_form = "yacc.XXXXXXX";
  14. #else
  15. char *temp_form = "t:yacc";
  16. #endif
  17.  
  18. int lineno;
  19. int outline;
  20.  
  21. char *action_file_name;
  22. char *defines_file_name;
  23. char *input_file_name = "";
  24. char *output_file_name;
  25. char *text_file_name;
  26. char *union_file_name;
  27. char *verbose_file_name;
  28.  
  29. FILE *action_file;    /*  a temp file, used to save actions associated    */
  30.             /*  with rules until the parser is written        */
  31. FILE *defines_file;    /*  y.tab.h                        */
  32. FILE *input_file;    /*  the input file                    */
  33. FILE *output_file;    /*  y.tab.c                        */
  34. FILE *text_file;    /*  a temp file, used to save text until all        */
  35.             /*  symbols have been defined                */
  36. FILE *union_file;    /*  a temp file, used to save the union            */
  37.             /*  definition until all symbol have been        */
  38.             /*  defined                        */
  39. FILE *verbose_file;    /*  y.output                        */
  40.  
  41. int nitems;
  42. int nrules;
  43. int nsyms;
  44. int ntokens;
  45. int nvars;
  46.  
  47. int   start_symbol;
  48. char  **symbol_name;
  49. short *symbol_value;
  50. short *symbol_prec;
  51. char  *symbol_assoc;
  52.  
  53. short *ritem;
  54. short *rlhs;
  55. short *rrhs;
  56. short *rprec;
  57. char  *rassoc;
  58. short **derives;
  59. char *nullable;
  60.  
  61. extern char *mktemp();
  62. extern char *getenv();
  63.  
  64.  
  65. done(k)
  66. int k;
  67. {
  68.     if (action_file) { fclose(action_file); unlink(action_file_name); }
  69.     if (text_file) { fclose(text_file); unlink(text_file_name); }
  70.     if (union_file) { fclose(union_file); unlink(union_file_name); }
  71.     exit(k);
  72. }
  73.  
  74.  
  75. onintr()
  76. {
  77.     done(1);
  78. }
  79.  
  80.  
  81. set_signals()
  82. {
  83. #ifdef SIGINT
  84.     if (signal(SIGINT, SIG_IGN) != SIG_IGN)
  85.     signal(SIGINT, onintr);
  86. #endif
  87. #ifdef SIGTERM
  88.     if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
  89.     signal(SIGTERM, onintr);
  90. #endif
  91. #ifdef SIGHUP
  92.     if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
  93.     signal(SIGHUP, onintr);
  94. #endif
  95. }
  96.  
  97.  
  98. usage()
  99. {
  100.     fprintf(stderr, "usage: %s [-dlrtv] [-b file_prefix] filename\n", myname);
  101.     exit(1);
  102. }
  103.  
  104.  
  105. getargs(argc, argv)
  106. int argc;
  107. char *argv[];
  108. {
  109.     register int i;
  110.     register char *s;
  111.  
  112.     if (argc > 0) myname = argv[0];
  113.     for (i = 1; i < argc; ++i)
  114.     {
  115.     s = argv[i];
  116.     if (*s != '-') break;
  117.     switch (*++s)
  118.     {
  119.     case '\0':
  120.         input_file = stdin;
  121.         if (i + 1 < argc) usage();
  122.         return;
  123.  
  124.     case '-':
  125.         ++i;
  126.         goto no_more_options;
  127.  
  128.     case 'b':
  129.         if (*++s)
  130.          file_prefix = s;
  131.         else if (++i < argc)
  132.         file_prefix = argv[i];
  133.         else
  134.         usage();
  135.         continue;
  136.  
  137.     case 'd':
  138.         dflag = 1;
  139.         break;
  140.  
  141.     case 'l':
  142.         lflag = 1;
  143.         break;
  144.  
  145.     case 'r':
  146.         rflag = 1;
  147.         break;
  148.  
  149.     case 't':
  150.         tflag = 1;
  151.         break;
  152.  
  153.     case 'v':
  154.         vflag = 1;
  155.         break;
  156.  
  157.     default:
  158.         usage();
  159.     }
  160.  
  161.     for (;;)
  162.     {
  163.         switch (*++s)
  164.         {
  165.         case '\0':
  166.         goto end_of_option;
  167.  
  168.         case 'd':
  169.         dflag = 1;
  170.         break;
  171.  
  172.         case 'l':
  173.         lflag = 1;
  174.         break;
  175.  
  176.         case 'r':
  177.         rflag = 1;
  178.         break;
  179.  
  180.         case 't':
  181.         tflag = 1;
  182.         break;
  183.  
  184.         case 'v':
  185.         vflag = 1;
  186.         break;
  187.  
  188.         default:
  189.         usage();
  190.         }
  191.     }
  192. end_of_option:;
  193.     }
  194.  
  195. no_more_options:;
  196.     if (i + 1 != argc) usage();
  197.     input_file_name = argv[i];
  198. }
  199.  
  200.  
  201. char *
  202. allocate(n)
  203. unsigned n;
  204. {
  205.     register char *p;
  206.  
  207.     p = NULL;
  208.     if (n)
  209.     {
  210.     p = CALLOC(1, n);
  211.     if (!p) no_space();
  212.     }
  213.     return (p);
  214. }
  215.  
  216. #ifdef AMIGA
  217. char *                                                                        
  218. TmpFileName(template)                                                         
  219. char *template;                                                               
  220. {                                                                             
  221.     static char Template[256];                                                
  222.     static unsigned short Idx;                                                
  223.                                                                               
  224.     sprintf(Template, "%s%08lx.TMP", template, (long)FindTask(NULL) + Idx++);
  225.     return(Template);                                                         
  226. }
  227. #endif
  228.  
  229. create_file_names()
  230. {
  231.     int i, len;
  232.     char *tmpdir;
  233.  
  234. #ifndef AMIGA
  235.     tmpdir = getenv("TMPDIR");
  236.     if (tmpdir == 0) tmpdir = "/tmp";
  237. #else
  238.     if (tmpdir == 0) tmpdir = "t:";
  239. #endif
  240.  
  241.     len = strlen(tmpdir);
  242.     i = len + 13;
  243.  
  244. #ifndef AMIGA
  245.     if (len && tmpdir[len-1] != '/')
  246.     ++i;
  247. #endif
  248.  
  249.     action_file_name = MALLOC(i);
  250.     if (action_file_name == 0) no_space();
  251.     text_file_name = MALLOC(i);
  252.     if (text_file_name == 0) no_space();
  253.     union_file_name = MALLOC(i);
  254.     if (union_file_name == 0) no_space();
  255.  
  256.     strcpy(action_file_name, tmpdir);
  257.     strcpy(text_file_name, tmpdir);
  258.     strcpy(union_file_name, tmpdir);
  259.  
  260. #ifndef AMIGA
  261.     if (len && tmpdir[len - 1] != '/')
  262.     {
  263.     action_file_name[len] = '/';
  264.     text_file_name[len] = '/';
  265.     union_file_name[len] = '/';
  266.     ++len;
  267.     }
  268. #endif
  269.  
  270.     strcpy(action_file_name + len, temp_form);
  271.     strcpy(text_file_name + len, temp_form);
  272.     strcpy(union_file_name + len, temp_form);
  273.  
  274.     action_file_name[len + 5] = 'a';
  275.     text_file_name[len + 5] = 't';
  276.     union_file_name[len + 5] = 'u';
  277.  
  278. #ifndef AMIGA
  279.     mktemp(action_file_name);
  280.     mktemp(text_file_name);
  281.     mktemp(union_file_name);
  282. #else
  283.     strcpy(action_file_name, TmpFileName(action_file_name));
  284.     strcpy(text_file_name, TmpFileName(text_file_name));
  285.     strcpy(union_file_name, TmpFileName(union_file_name));
  286. #endif
  287.  
  288.     len = strlen(file_prefix);
  289.     if (dflag)
  290.     {
  291.     /*  the number 7 below is the size of ".tab.h"; sizeof is not used  */
  292.     /*  because of a C compiler that thinks sizeof(".tab.h") == 6        */
  293.     defines_file_name = MALLOC(len + 7);
  294.     if (defines_file_name == 0) no_space();
  295.     strcpy(defines_file_name, file_prefix);
  296.     strcpy(defines_file_name + len, DEFINES_SUFFIX);
  297.     }
  298.  
  299.     output_file_name = MALLOC(len + 7);
  300.     if (output_file_name == 0) no_space();
  301.     strcpy(output_file_name, file_prefix);
  302.     strcpy(output_file_name + len, OUTPUT_SUFFIX);
  303.  
  304.     if (vflag)
  305.     {
  306.     verbose_file_name = MALLOC(len + 8);
  307.     if (verbose_file_name == 0) no_space();
  308.     strcpy(verbose_file_name, file_prefix);
  309.     strcpy(verbose_file_name + len, VERBOSE_SUFFIX);
  310.     }
  311. }
  312.  
  313.  
  314. open_files()
  315. {
  316.     create_file_names();
  317.  
  318.     if (input_file == 0)
  319.     {
  320.     input_file = fopen(input_file_name, "r");
  321.     if (input_file == 0) open_error(input_file_name);
  322.     }
  323.  
  324.     action_file = fopen(action_file_name, "w");
  325.     if (action_file == 0) open_error(action_file_name);
  326.  
  327.     text_file = fopen(text_file_name, "w");
  328.     if (text_file == 0) open_error(text_file_name);
  329.  
  330.     if (vflag)
  331.     {
  332.     verbose_file = fopen(verbose_file_name, "w");
  333.     if (verbose_file == 0) open_error(verbose_file_name);
  334.     }
  335.  
  336.     if (dflag)
  337.     {
  338.     defines_file = fopen(defines_file_name, "w");
  339.     if (defines_file == 0) open_error(defines_file_name);
  340.     union_file = fopen(union_file_name, "w");
  341.     if (union_file ==  0) open_error(union_file_name);
  342.     }
  343.  
  344.     output_file = fopen(output_file_name, "w");
  345.     if (output_file == 0) open_error(output_file_name);
  346. }
  347.  
  348.  
  349. int
  350. main(argc, argv)
  351. int argc;
  352. char *argv[];
  353. {
  354.     set_signals();
  355.     getargs(argc, argv);
  356.     open_files();
  357.     reader();
  358.     lr0();
  359.     lalr();
  360.     make_parser();
  361.     verbose();
  362.     output();
  363.     done(0);
  364.     /*NOTREACHED*/
  365. }
  366.